package net.dreamlu.qa.validator;

import net.dreamlu.qa.model.User;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

public class UserValidator extends Validator {
	
	protected void validate(Controller controller) {
		setShortCircuit(true);
		validateEmail("user.email", "errorMail", "1");
		validateString("user.password", 6, 20, "errorPwd", "1");
		validateString("user.login_name", 2, 20, "errorName", "1");
		
		User user = User.dao.findFirst("select 1 from user_info where email = ? or login_name = ? or email_verify != 1", controller.getPara("user.email"), controller.getPara("user.login_name"));
		if(user != null){
			addError("errorMail", "1");
		}
	}
	
	protected void handleError(Controller controller) {
		controller.keepModel(User.class);
		controller.render("/account/register.jsp");
	}
}